<template>
  <div class="order-wrap">
    <el-form class="form">
      <el-form-item label-width="100px" label="信息名称：">
        <el-input v-model="ordername" type="text"/>
      </el-form-item>
      <el-form-item label-width="100px" label="发布人：">
        <el-input v-model="userid" type="text"/>
      </el-form-item>
      <el-form-item label-width="120px" label="发布人手机：">
        <el-input v-model="phone" type="text"/>
      </el-form-item>
      <el-form-item label-width="100px" label="发布城市：">
        <area-cascader v-model="releaseCity" :level="1" :data="pcaa" type="text" placeholder="请选择省/市/区" @change="hanleCheckCity"/>
      </el-form-item>
      <el-form-item label-width="100px" label="信息类型：">
        <el-select v-model="infotype" placeholder="请选择">
          <el-option
            v-for="item in infos"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="100px" label="支付方式：">
        <el-select v-model="paytype" placeholder="请选择">
          <el-option
            v-for="item in payType"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="100px" label="消费时间：">
        <el-date-picker
          v-model="pickDate"
          :picker-options="pickerOptions2"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="left"/>
      </el-form-item>
      <el-form-item label-width="50px">
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" :summary-method="getSummaries" sum-text="消费金额合计" style="width: 100%" border show-summary>
      <el-table-column prop="index" label="序号" width="180"/>
      <el-table-column prop="ordername" label="信息名称/企业名称" width="180">
        <template slot-scope="scope">
          <div>{{ scope.row.ordername === 'null' ? '' : scope.row.ordername }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="releasecity" label="发布城市" width="180"/>
      <el-table-column prop="infotype" label="类型" width="180">
        <template slot-scope="scope">
          <div>{{ scope.row.infotype === 1? '租赁' : scope.row.infotype === 2 ? '销售':'维修' }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="releaseuser" label="发布人" width="180"/>
      <el-table-column prop="phone" label="发布人手机" width="180"/>
      <el-table-column prop="paytype" label="支付方式" width="180">
        <template slot-scope="scope">
          <div>{{ scope.row.paytype === 1? '现金' : '年卡' }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="consumptionmoney" sortable label="消费金额" width="180"/>
      <el-table-column prop="optdate" label="消费时间" width="180">
        <template slot-scope="scope">
          <div>{{ new Date(scope.row.optdate).toLocaleString() }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="duration" label="购买时长" width="180">
        <template slot-scope="scope">
          <div>{{ scope.row.duration }}月</div>
        </template>
      </el-table-column>
      <el-table-column prop="transactionno" label="单号" width="180"/>
    </el-table>
    <pagination v-show="total>10" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog
      :visible.sync="centerDialogVisible"
      title="提示"
      width="30%"
      center>
      <span>确认删除？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { pcaa } from 'area-data'
import { orderList } from '@/api/order'
import { time } from '@/utils/index.js'
export default {
  name: 'Publish',
  components: {
    Pagination
  },
  data() {
    return {
      publish: '',
      publishId: '',
      releaseCity: [],
      phone: '',
      pcaa: pcaa,
      city: '',
      infos: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 1,
          label: '租赁'
        },
        {
          value: 2,
          label: '销售'
        },
        {
          value: 3,
          label: '维修'
        }
      ],
      payValue: '',
      payType: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 1,
          label: '现金'
        },
        {
          value: 2,
          label: '年卡'
        }
      ],
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      pickDate: '',
      total: 0,
      listQuery: {
        page: 1,
        limit: 10
      },
      centerDialogVisible: false,
      iDisplayStart: 0,
      iDisplayLength: 10,
      ordername: '',
      userid: '',
      paytype: '',
      infotype: '',
      ordertype: '',
      releasecity: '',
      fromtime: '',
      totime: '',
      list: [],
      totalcostamount: 0
    }
  },
  computed: {
    tableData() {
      const result = []
      this.list.map((item, index) => {
        // const idx = this.type.findIndex((subitem) => {
        //   return item.typecode === subitem.code
        // })
        item = Object.assign({}, item, { index: `${(this.listQuery.page - 1) * this.listQuery.limit + 1 + index}`, edit: false })
        result.push(item)
      })
      return result
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    handleDelete(scope) {
      this.centerDialogVisible = true
    },
    getList(prop) {
      this.listQuery.page = prop.page
      this.listQuery.limit = prop.limit
      this.iDisplayStart = (prop.page - 1) * prop.limit
      this.iDisplayLength = prop.limit
      this.getOrderList()
    },
    hanleCheckCity() {
      console.log(this.city)
    },
    getSummaries(params) {
      const SUM = {
        index: 0,
        text: '消费金额合计',
        prop: 'consumptionmoney'
      }
      const { columns } = params
      const sums = []
      columns.map((item, index) => {
        if (index === SUM.index) {
          sums[index] = SUM.text
        }
        if (item.property === SUM.prop) {
          sums[index] = `${this.totalcostamount}元`
        }
        sums.push('')
      })
      return sums
    },
    getOrderList() {
      let fromtime, totime, releasecity
      let { infotype } = this
      const { iDisplayStart, iDisplayLength, ordername, userid, paytype, ordertype, releaseCity, pickDate, phone } = this
      if (infotype === '租赁') {
        infotype = 1
      }
      if (!pickDate.length) {
        fromtime = ''
        totime = ''
      } else {
        fromtime = `${time(pickDate[0])} ${new Date(pickDate[0]).toTimeString().substring(0, 8)}`
        totime = `${time(pickDate[1])} ${new Date(pickDate[1]).toTimeString().substring(0, 8)}`
      }
      if (releaseCity.length) {
        releasecity = `${releaseCity[0]}${releaseCity[1]}${releaseCity[2]}`
      } else {
        releasecity = ''
      }
      orderList({ iDisplayStart, iDisplayLength, ordername, userid, phone, paytype, infotype, ordertype, releasecity, fromtime, totime })
        .then((res) => {
          const data = res.data
          this.list = data
          this.total = res.total
          this.totalcostamount = res.totalcostamount
        }).catch((e) => {
          this.$message.error({
            message: '请稍后再试',
            duration: 1000
          })
        })
    },
    handleSearch() {
      this.getOrderList()
    }
  }
}
</script>
<style lang="scss" scoped>
.order-wrap {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  .form {
    display: flex;
    flex-wrap: wrap;
  }
  .city-cascader {
    .area-select.large{
      width: 200px;
      background-color: #000;
      .area-selected-trigger {
        width: 200px;
        background-color: red;
      }
    }
  }
}
</style>
